package com.seari.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.Utils.HandlerNullUtils;
import com.seari.main.StartUpClass;
import com.seari.mapper.DdMapper;
import com.seari.pojo.MessageBean;

@Controller
public class DdController {

	@Autowired
	private DdMapper ddMapper;

	@RequestMapping("device/getEnergyRatio")
	@ResponseBody
	public MessageBean getEnergyRatio() {

		MessageBean messageBean = new MessageBean();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, String> ddpercent =  new HashMap<String, String>();
		Map<String, Object> ddPercentObj = new HashMap<String, Object>();
		try {
			if ("test".equals(StartUpClass.energyRatio)) {
				
				String[] degree = {"67580","25349","16862","33774","16890","50811","101098","101130"};
				String[] type = {"一、二级负荷","三级负荷","冷水机组","售检票","屏蔽门","照明","环控","电梯"};
				
				ddPercentObj.put("type", type);
				ddPercentObj.put("degree", degree);
				dataMap.put("ddPercentObj", ddPercentObj);
				messageBean.setType("success");
				messageBean.setMessage("能耗占比信息获取成功");
				messageBean.setData(dataMap);
			} else {
				ddpercent = ddMapper.getDdpercent();

				List<String> typeStrList = null;
				List<String> degreeStrList = null;
				if(!HandlerNullUtils.objectIsNull(ddpercent)){
					List<String> type = Arrays.asList(ddpercent.get("type").split(","));
					List<String> degree = Arrays.asList(ddpercent.get("degree").split(","));
					typeStrList = new ArrayList<String>(type);
					degreeStrList = new ArrayList<String>(degree);
				}
				
				for (int i = 0; i < typeStrList.size()-1; i++) {
					
					if ("总用电".equals(typeStrList.get(i))) {
						
						typeStrList.remove(i);
						degreeStrList.remove(i);
						i --;
					}
				}

			
				ddPercentObj.put("type", typeStrList);
				ddPercentObj.put("degree", degreeStrList);
				dataMap.put("ddPercentObj", ddPercentObj);
				messageBean.setType("success");
				messageBean.setMessage("能耗占比信息获取成功");
				messageBean.setData(dataMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setMessage("能耗占比信息获取失败");
		}

		return messageBean;
	}

}
